<?php   
/*  
 */  
class ip {   
    public function getIp() {   
        if ($_SERVER['HTTP_CLIENT_IP'] && $_SERVER['HTTP_CLIENT_IP']!='unknown') {   
            $ip = $_SERVER['HTTP_CLIENT_IP'];   
        } elseif ($_SERVER['HTTP_X_FORWARDED_FOR'] && $_SERVER['HTTP_X_FORWARDED_FOR']!='unknown') {   
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];   
        } else {   
            $ip = $_SERVER['REMOTE_ADDR'];   
        }   
        return $ip;   
    }   
       
    public function ipToInt($ip) {   
        $ips = explode('.',$ip);   
        if (count($ips)>=4) {   
            $int = $ips[0]*256*256*256+$ips[1]*256*256+$ips[2]*256+$ips[3];   
        } else {   
            throw new Exception('ip is error');   
        }   
        return $int;   
    }   
       
    public function isIn($startIp, $endIp, $ip) {   
        $start = Ip::ipToInt($startIp);   
        $end = Ip::ipToInt($endIp);   
        $ipInt = Ip::ipToInt($ip);   
        $result = false;   
        if ($ipInt>=$start && $ipInt<=$end) {   
            $result = true;   
        }   
        return $result;   
    }   
}   
?>  
